function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let layedit = layui.layedit;
    layui.$(function () {
        layui.$("#vercodeImage").click(function () {
            layui.$("#vercodeImage")[0].src ='/captcha/gif.action?'+Math.random();
        })
    })

    console.log("来源页面：" + window.document.referrer);

    if (self != top) {
        top.location.href = window.location.href;
    }

    // 提交
    form.on('submit(lay-user-login-submit)', function (obj) {
        var redirectUrl = getQueryString("redirectUrl");
        var redirectUrl2 = $('#redirectUrl').val();

        if (redirectUrl2 == '' || redirectUrl2 == null) {
            redirectUrl2 = redirectUrl;
        }

        if (redirectUrl2 != '' && redirectUrl2 != null) {
            if (redirectUrl2.indexOf("cc-server") == -1 && redirectUrl2.indexOf("cc-web") == -1) {
                redirectUrl2 = "";
            }
        }

        let staffName = $('#LAY-user-login-username').val();
        let password = $('#LAY-user-login-password').val();
        let vercode = '';
        if(staffName==null||staffName==''){
            layer.msg('账号不能为空');
            return false;
        }
        if(password==null||password==''){
            layer.msg('密码不能为空');
            return false;
        }
        if(!$("#vercode").is(":hidden")){
            vercode = $('#LAY-user-login-vercode').val();
            if(vercode==null||vercode==''){
                layer.msg('验证码不能为空');
                return false;
            }
        }
        let staffLogin = {
            staffName: $('#LAY-user-login-username').val(),
            password: $('#LAY-user-login-password').val(),
            vercode:vercode,
            redirectUrl: redirectUrl2
        };

        let url = '/cas/login.action';
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(staffLogin),
            success: function (json) {
                args = json;
                if (json.status == 200) {
                    window.location.href = json.id;
                } else {
                    if (json.status == 41004) {
                        // layui.$("#LAY-user-login-username").val('');
                        layui.$("#LAY-user-login-password").val('');
                        layui.$("#LAY-user-login-vercode").val('');
                        layui.$("#vercodeImage")[0].src ='/captcha/gif.action?'+Math.random();
                        if(json.statusCode.indexOf('PWD_E')!=-1&&json.statusCode.length>5){
                            layui.$("#vercode").show();
                        }else{
                            layui.$("#vercode").hide();
                        }
                    }
                    layer.msg(json.message);
                }
            }
        })
        return false;
    });

    // 对外暴露的接口
    exports('art_cas_login', {});
});

var args;